
package edu.weber.bookstore.controllers;

import edu.weber.bookstore.database.UserCart;
import edu.weber.bookstore.models.UserCartIndexModel;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * 
 * 
 */
public class UserCartsController extends AppController
{
	/**
	 * Page to view cart items
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "/UserCarts/index")
	public ModelAndView index(HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		UserCart uc = UserCart.find(AppController.getSessionId(request));
		
		float total = 0;
		if(uc != null)
		{
			for(int i = 0; i < uc.getUserCartBooks().size(); i++)
			{
				total += uc.getUserCartBooks().get(i).getBook().getPrice() * uc.getUserCartBooks().get(i).getQty();
			}
		}
		UserCartIndexModel ucim = new UserCartIndexModel(uc, total);
		return new ModelAndView("user_carts/index", "model", ucim.getMap());
	}
}
